set more off
use "C:\Users\Andy Baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear
gen respond=1
fillin idtelefone wave
recode respond .=0
xtset idtelefone wave
sort idtelefone wave

*Before reshaping, generate some variables
	*vote choice
recode vb1 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=15 11=11 12=. 13=17 14=17 .a=16 .b=. .=., gen(vote)
label define vote 1 Dilma 2 Aecio 3 Campos 4 Marina 5 "Everaldo Pereira" 6 "Eduardo Jorge" 7 "Luciana Genro" 11 "Levy Fidelix" 15 Other 17 Abstain 16 DK
label values vote vote

	*Party intensity
recode vb10 1=1 2=0 .a=0 .b=0 .=. , gen(partyintense)
replace partyintense=.5 if vb12==1

	*Number of discussants
replace disc5b=. if disc5a==.a | disc5a==.b | disc5a==.c | disc5a==.
replace disc6b=. if disc6a==.a | disc6a==.b | disc6a==.c | disc6a==.
 
recode disc5a 1/8=1 9/10=0 .a=0 .b=0, gen(disc_family)
recode disc6a 1/8=1 10=0  .a=0 .b=0, gen(disc_friend)
egen disc_total=rsum(disc_family disc_friend)
replace disc_total=. if wave==1 | wave==6 | wave==7

rename disc5b disc_family_vote
rename disc6b disc_friend_vote
replace disc_family_vote = disc5b2 if wave==6 | wave==7 
replace disc_friend_vote = disc6b2 if wave==6 | wave==7 
recode disc_family_vote 13=14 .b=.a
recode disc_friend_vote 13=14 .b=.a

*THRESHOLD (I use the candidate traits to suplement since feeling therm was only asked in first wave)
replace vb8c=vb8c_marina if wave>2
replace vb9c=vb9c_marina if wave>2

	*For Dilma voters
egen tempaeciomarina=rowmean(vb8b vb9b vb8c vb9c)
egen tempdilma=rowmean(vb8a vb9a)
gen dilma_therm=tempdilma-tempaeciomarina

	*For Aecio voters
egen tempdilmamarina=rowmean(vb8a vb9a vb8c vb9c)
egen tempaecio=rowmean(vb8b vb9b)
gen aecio_therm=tempaecio-tempdilmamarina

	*For Marina voters (Campos in wave 2)
egen tempdilmaaecio=rowmean(vb8a vb9a vb8b vb9b)
egen tempmarina=rowmean(vb8c vb9c)
gen marina_therm=tempmarina-tempdilmaaecio

gen threshold=dilma_therm if vb1==1
replace threshold=aecio_therm if vb1==2
replace threshold=marina_therm if vb1==3
replace threshold=marina_therm if vb1==4
*replace threshold=0 if threshold<0
replace threshold=(threshold+10)/20

drop temp* *_therm

replace ft2=ft2[_n-1] if wave==2
replace ft3=ft3[_n-1] if wave==2
replace ft5=ft5[_n-1] if wave==2
replace ft6=ft6[_n-1] if wave==2
replace ft7=ft7[_n-1] if wave==2
replace ft2=ft2[_n-2] if wave==3
replace ft3=ft3[_n-2] if wave==3
replace ft5=ft5[_n-2] if wave==3
replace ft6=ft6[_n-2] if wave==3
replace ft7=ft7[_n-2] if wave==3
replace ft2=ft2[_n-3] if wave==4
replace ft3=ft3[_n-3] if wave==4
replace ft5=ft5[_n-3] if wave==4
replace ft6=ft6[_n-3] if wave==4
replace ft7=ft7[_n-3] if wave==4
replace ft2=ft2[_n-4] if wave==5
replace ft3=ft3[_n-4] if wave==5
replace ft5=ft5[_n-4] if wave==5
replace ft6=ft6[_n-4] if wave==5
replace ft7=ft7[_n-4] if wave==5

	*For Dilma voters
egen tempaeciomarina=rowmean(ft2 ft5)
egen tempdilma=rowmean(ft3)
gen dilma_therm=tempdilma-tempaeciomarina

	*For Aecio voters
egen tempdilmamarina=rowmean(ft3 ft5)
egen tempaecio=rowmean(ft2)
gen aecio_therm=tempaecio-tempdilmamarina

	*For Marina voters
egen tempdilmaaecio=rowmean(ft3 ft2)
egen tempmarina=rowmean(ft5)
gen marina_therm=tempmarina-tempdilmaaecio

gen thresholdft=dilma_therm if vb1==1
replace thresholdft=aecio_therm if vb1==2
replace thresholdft=marina_therm if vb1==3
replace thresholdft=marina_therm if vb1==4
*replace thresholdft=0 if thresholdft<0
replace thresholdft=(thresholdft+10)/20

rename l1 ideo

keep idtelefone wave respond vote disc_* pk* weight_* partyintense med1 p10 hpge1_1 hpge1_2 threshold thresholdft vb11 vb13 dilma_therm aecio_therm marina_therm educ ideo ft1-ft7 uf disc5a 
reshape wide respond vote disc_* weight_* pk* partyintense med1 p10 hpge1_1 hpge1_2 threshold thresholdft vb11 vb13 dilma_therm aecio_therm marina_therm educ ideo ft1-ft7 uf disc5a, i(idtelefone) j(wave)
svyset [pweight=weight_censo1], psu(uf1)

*cited in opening anecdote
egen pt=anymatch(vb111 vb112 vb113 vb114 vb115 vb116), value(1501)
svy: tab vote6 pt if respond6==1

egen psdb=anymatch(vb111 vb112 vb113 vb114 vb115 vb116), value(1503)
svy: tab vote6 psdb if respond6==1

egen psb=anymatch(vb111 vb112 vb113 vb114 vb115 vb116), value(1504)
svy: tab vote6 psb if respond6==1



*Of all opinionated voters (does not include DK in the denominator, and also excludes none/abstention or nr)
*All combos: 1 to 2, 1 to 3, 1 to 4, 1 to 5, 1 to 6, 2 to 4, 2 to 5, 2 to 6, 3 to 4, 3 to 5, 3 to 6, 4 to 6, 5 to 6.
			*first wave Marina voters (but not Campos voters) recoded to .
gen change12=0 if respond1==1 & respond2==1 & vote1~=4 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote2~=17 & vote2~=16 & vote2~=6 & vote2~=.
replace change12=1 if vote1~=vote2 & vote1<16 & vote2<16 & change12==0
bysort change12: tab vote1 vote2 if respond2==1, m
svy: tab change12

				*Campos died between waves 2 and 3 on 8/13
			*first and third wave Campos voters recoded to .
gen change13=0 if respond1==1 & respond3==1 & vote1~=3 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote3~=3 & vote3~=17 & vote3~=16 & vote3~=.
replace change13=1 if vote1~=vote3 & vote1<16 & vote3<16 & change13==0
bysort change13: tab vote1 vote3 if respond3==1, m
svy: tab change13

egen change123=rsum(change12 change13)
replace change123=. if change12==. & change13==. 
svy: tab change123 

gen change14=0 if respond1==1 & respond4==1 & vote1~=3 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote4~=7 & vote4~=6 & vote4~=17 & vote4~=16 & vote4~=.
replace change14=1 if vote1~=vote4 & vote1<16 & vote4<16 & change14==0
bysort change14: tab vote1 vote4 if respond4==1, m
svy: tab change14

gen change15=0 if respond1==1 & respond5==1 & vote1~=3 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote5~=7 & vote5~=6 & vote5~=17 & vote5~=16 & vote5~=.
replace change15=1 if vote1~=vote5 & vote1<16 & vote5<16 & change15==0
bysort change15: tab vote1 vote5 if respond5==1, m
svy: tab change15

			*second wave Campos voters recoded to .
gen change24=0 if respond2==1 & respond4==1 & vote2~=3 & vote2~=17 & vote2~=16 & vote2~=. & vote4~=17 & vote4~=16 & vote4~=.
replace change24=1 if vote2~=vote4 & vote2<16 & vote4<16 & change24==0
bysort change24: tab vote2 vote4, m
svy: tab change24

 gen change25=0 if respond2==1 & respond5==1 & vote2~=3 & vote2~=17 & vote2~=. & vote5~=17 & vote5~=. & vote2~=16 & vote5~=16
replace change25=1 if vote2~=vote5 & vote2<16 & vote5<16 & change25==0
bysort change25: tab vote2 vote5, m
svy: tab change25

gen change26=0 if respond2==1 & respond6==1 & vote2~=3 & vote2~=17 & vote2~=. & vote2~=16 & vote6~=17 & vote6~=. & vote6~=16
replace change26=1 if vote2~=vote6 & vote2<16 & vote6<16 & change26==0
bysort change26: tab vote2 vote6, m
svy: tab change26

gen change16=0 if respond1==1 & respond6==1 & vote1~=3 & vote1~=17 & vote1~=11 & vote1~=16 & vote1~=16 & vote1~=. & vote6~=17 & vote6~=. & vote6~=16
replace change16=1 if vote1~=vote6 & vote1<16 & vote6<16 & change16==0
bysort change16: tab vote1 vote6 if respond6==1, m
svy: tab change16

*--------------- choice set remains the same after wave 2, so things get easier
gen change34=0 if respond3==1 & respond4==1 & vote3~=17 & vote3~=. & vote4~=17 & vote4~=. & vote3~=16 & vote4~=16
replace change34=1 if vote3~=vote4 & vote3<16 & vote4<16 & change34==0
bysort change34: tab vote3 vote4, m
svy: tab change34

gen change35=0 if respond3==1 & respond5==1 & vote3~=17 & vote3~=. & vote5~=17 & vote5~=. & vote3~=16 & vote5~=16
replace change35=1 if vote3~=vote5 & vote3<16 & vote5<16 & change35==0
bysort change35: tab vote3 vote5, m
svy: tab change35

egen change2345=rsum(change24 change25 change34 change35)
replace change2345=. if change24==. & change25==. & change34==. & change35==.
svy: tab change2345

gen change36=0 if respond3==1 & respond6==1 & vote3~=17 & vote3~=. & vote3~=16 & vote6~=17 & vote6~=. & vote6~=16
replace change36=1 if vote3~=vote6 & vote3<16 & vote6<16 & change36==0
bysort change36: tab vote3 vote6, m
svy: tab change36

	*for chapter 3
*gen major_party_not_strategic36=1 if (vote3==1 & vote6==2) | (vote3==2 & vote6==1)
*replace major_party_not_strategic36=0 if change36~=. & major_party_not_strategic36==.
*gen strategic36=1 if (vote3==4 & vote6==1) |  (vote3==4 & vote6==2) | (vote3==5 & vote6==1) |  (vote3==5 & vote6==2) | (vote3==6 & vote6==1) |  (vote3==6 & vote6==2)  | (vote3==7 & vote6==1) |  (vote3==7 & vote6==2) | (vote3==11 & vote6==1) |  (vote3==11 & vote6==2) | (vote3==15 & vote6==1) |  (vote3==15 & vote6==2)
*replace strategic36=0 if change36~=. & strategic36==.
gen strategic36inbrazil=1 if (vote3==1 & vote6==4) | (vote3==1 & vote6==2) | (vote3==1 & vote6==4) | (vote3==5 & vote6==2) | (vote3==5 & vote6==4) | (vote3==6 & vote6==2) | (vote3==6 & vote6==4) | (vote3==7 & vote6==2) | (vote3==7 & vote6==4) | (vote3==15 & vote6==2) | (vote3==15 & vote6==4) 
replace strategic36inbrazil=0 if change36~=. & strategic36inbrazil==.
	*SE cell is percent of switchers switching between top two, couldn't be strategic
*svy: tab change36 major_party_not_strategic36, row
	*SE cell is percent of switchers possibly strategic
*svy: tab change36 strategic36, row cel
	*SE cell is percent of switchers possibly strategic in brazil
svy: tab change36 strategic36inbrazil, row cel

gen change46=0 if respond4==1 & respond6==1 & vote4~=17 & vote4~=. & vote4~=16  & vote6~=17 & vote6~=. & vote6~=16
replace change46=1 if vote4~=vote6 & vote4<16 & vote6<16 & change46==0
bysort change46: tab vote4 vote6, m
svy: tab change46

	*for chapter 2
gen strategic46inbrazil=1 if (vote4==1 & vote6==4) | (vote4==1 & vote6==2) | (vote4==1 & vote6==4) | (vote4==5 & vote6==2) | (vote4==5 & vote6==4) | (vote4==6 & vote6==2) | (vote4==6 & vote6==4) | (vote4==7 & vote6==2) | (vote4==7 & vote6==4) | (vote4==15 & vote6==2) | (vote4==15 & vote6==4) 
replace strategic46inbrazil=0 if change46~=. & strategic46inbrazil==.
svy: tab change46 strategic46inbrazil, row cel

gen change56=0 if respond5==1 & respond6==1 & vote5~=17 & vote5~=. & vote6~=17 & vote6~=.  & vote5~=16 & vote6~=16
replace change56=1 if vote5~=vote6 & vote5<16 & vote6<16 & change56==0
bysort change56: tab vote5 vote6, m
svy: tab change56

	*for chapter 2
gen strategic56inbrazil=1 if (vote5==1 & vote6==4) | (vote5==1 & vote6==2) | (vote5==1 & vote6==4) | (vote5==5 & vote6==2) | (vote5==5 & vote6==4) | (vote5==6 & vote6==2) | (vote5==6 & vote6==4) | (vote5==7 & vote6==2) | (vote5==7 & vote6==4) | (vote5==15 & vote6==2) | (vote5==15 & vote6==4) 
replace strategic56inbrazil=0 if change56~=. & strategic56inbrazil==.
svy: tab change56 strategic56inbrazil, row cel

egen change456=rsum(change46 change56)
replace change456=. if change46==. & change56==. 
svy: tab change456

egen changedatall=rsum(change14 change15 change46 change56)
replace changedatall=. if respond6==0
replace changedatall=. if respond4==0 & respond5==0
replace changedatall=. if vote1==3
replace changedatall=. if vote1>5
replace changedatall=. if vote4>6 & vote4~=.
replace changedatall=. if vote5>6 & vote5~=.
replace changedatall=. if vote6>6
recode changedatall 2=1

	*estimate in opening anecdote
tab change2345 change16

*INDECISOS
*All combos: 1 to 2, 1 to 3, 1 to 4, 1 to 5, 1 to 6, 2 to 4, 2 to 5, 2 to 6, 3 to 4, 3 to 5, 3 to 6, 4 to 6, 5 to 6.
			*first wave Marina voters (but not Campos voters) recoded to .
gen switch12=0 if respond1==1 & respond2==1 & vote1~=4 & vote1~=11 & vote1~=. & vote2~=17 & vote2~=16 & vote2~=6 & vote2~=.
replace switch12=1 if vote1~=vote2 & vote2<16 & switch12==0
replace switch12=2 if (vote1==17 & switch12~=.) | (vote1==16 & switch12~=.)
bysort switch12: tab vote1 vote2 if respond2==1, m
svy: tab switch12

				*Campos died between waves 2 and 3 on 8/13
			*first and third wave Campos voters recoded to .
gen switch13=0 if respond1==1 & respond3==1 & vote1~=3 & vote1~=11 & vote1~=. & vote3~=3 & vote3~=17 & vote3~=16 & vote3~=.
replace switch13=1 if vote1~=vote3 & vote3<16 & switch13==0
replace switch13=2 if (vote1==17 & switch13~=.) | (vote1==16 & switch13~=.)
bysort switch13: tab vote1 vote3 if respond3==1, m
svy: tab switch13

gen switch14=0 if respond1==1 & respond4==1 & vote1~=3 & vote1~=11 & vote1~=. & vote4~=7 & vote4~=6 & vote4~=17 & vote4~=16 & vote4~=.
replace switch14=1 if vote1~=vote4 & vote4<16 & switch14==0
replace switch14=2 if (vote1==17 & switch14~=.) | (vote1==16 & switch14~=.)
bysort switch14: tab vote1 vote4 if respond4==1, m
svy: tab switch14

gen switch15=0 if respond1==1 & respond5==1 & vote1~=3 & vote1~=11 & vote1~=. & vote5~=7 & vote5~=6 & vote5~=17 & vote5~=16 & vote5~=.
replace switch15=1 if vote1~=vote5 & vote5<16 & switch15==0
replace switch15=2 if (vote1==17 & switch15~=.) | (vote1==16 & switch15~=.)
bysort switch15: tab vote1 vote5 if respond5==1, m
svy: tab switch15

			*second wave Campos voters recoded to .
gen switch24=0 if respond2==1 & respond4==1 & vote2~=3 & vote2~=. & vote4~=17 & vote4~=16 & vote4~=.
replace switch24=1 if vote2~=vote4 & vote4<16 & switch24==0
replace switch24=2 if (vote2==17 & switch24~=.) | (vote2==16 & switch24~=.)
bysort switch24: tab vote2 vote4, m
svy: tab switch24

gen switch25=0 if respond2==1 & respond5==1 & vote2~=3 & vote2~=. & vote5~=17 & vote5~=. & vote5~=16
replace switch25=1 if vote2~=vote5 & vote5<16 & switch25==0
replace switch25=2 if (vote2==17 & switch25~=.) | (vote2==16 & switch25~=.)
bysort switch25: tab vote2 vote5, m
svy: tab switch25

gen switch26=0 if respond2==1 & respond6==1 & vote2~=3 & vote2~=. & vote6~=17 & vote6~=. & vote6~=16
replace switch26=1 if vote2~=vote6 & vote6<16 & switch26==0
replace switch26=2 if (vote2==17 & switch26~=.) | (vote2==16 & switch26~=.)
bysort switch26: tab vote2 vote6, m
svy: tab switch26

gen switch16=0 if respond1==1 & respond6==1 & vote1~=3 & vote1~=11 & vote1~=. & vote6~=17 & vote6~=. & vote6~=16
replace switch16=1 if vote1~=vote6 & vote6<16 & switch16==0
replace switch16=2 if (vote1==17 & switch16~=.) | (vote1==16 & switch16~=.)
bysort switch16: tab vote1 vote6 if respond6==1, m
svy: tab switch16

*--------------- choice set remains the same after wave 2, so things get easier
gen switch34=0 if respond3==1 & respond4==1 & vote3~=. & vote4~=17 & vote4~=. & vote4~=16
replace switch34=1 if vote3~=vote4 & vote4<16 & switch34==0
replace switch34=2 if (vote3==17 & switch34~=.) | (vote3==16 & switch34~=.)
bysort switch34: tab vote3 vote4, m
svy: tab switch34

gen switch35=0 if respond3==1 & respond5==1 & vote3~=. & vote5~=17 & vote5~=. & vote5~=16
replace switch35=1 if vote3~=vote5 & vote5<16 & switch35==0
replace switch35=2 if (vote3==17 & switch35~=.) | (vote3==16 & switch35~=.)
bysort switch35: tab vote3 vote5, m
svy: tab switch35

gen switch36=0 if respond3==1 & respond6==1 & vote3~=. & vote6~=17 & vote6~=. & vote6~=16
replace switch36=1 if vote3~=vote6 & vote6<16 & switch36==0
replace switch36=2 if (vote3==17 & switch36~=.) | (vote3==16 & switch36~=.)
bysort switch36: tab vote3 vote6, m
svy: tab switch36

gen switch46=0 if respond4==1 & respond6==1 & vote4~=. & vote6~=17 & vote6~=. & vote6~=16
replace switch46=1 if vote4~=vote6 & vote6<16 & switch46==0
replace switch46=2 if (vote4==17 & switch46~=.) | (vote4==16 & switch46~=.)
bysort switch46: tab vote4 vote6, m
svy: tab switch46

gen switch56=0 if respond5==1 & respond6==1 & vote5~=. & vote6~=17 & vote6~=.  & vote6~=16
replace switch56=1 if vote5~=vote6 & vote6<16 & switch56==0
replace switch56=2 if (vote5==17 & switch56~=.) | (vote5==16 & switch56~=.)
bysort switch56: tab vote5 vote6, m
svy: tab switch56

egen switch456=rsum(switch46 switch56)
replace switch456=. if switch46==. & switch56==. 
svy: tab switch456


			*in all three waves 
egen switchedatall=rsum(switch14 switch15 switch46 switch56)
replace switchedatall=1 if vote1==.
replace switchedatall=. if respond6==0
replace switchedatall=. if respond4==0 & respond5==0
replace switchedatall=. if vote1==3
replace switchedatall=. if vote1==11
replace switchedatall=. if vote6>5
recode switchedatall 2/3=1

	*DISCUSSANT IVS
/*THIS IS TRICKY AS HELL
In Wave 6 and Wave 7, respondents were asked about the first and second round presidential vote choices of their most recently named discussants, 
identified by the relationship. Because of the close timing of waves, discussants named in Wave 5 were not asked about in Waves 6 and 7. 
So, means that Wave 6 R’s were asked about wave 4 discussants if they participated in wave 4, and wave 2 or 3 discussants if they did not 
participate in wave 4.
*/
	
	*Generate discussant variables
		*Count discussants
egen disc_total23=rsum(disc_total2 disc_total3)
replace disc_total23 =. if respond2==0 & respond3==0
replace disc_total23 =. if respond2==1 & respond3==1

egen disc_total45=rsum(disc_total4 disc_total5)
replace disc_total45 =. if respond4==0 & respond5==0
replace disc_total45 =. if respond4==1 & respond5==1

		*Heterogeneity 
			*for Strong disagreement (only opinionated discussants)
				*wave 2
recode disc_family_vote2 disc_friend_vote2 (1=1) (2/14=0) (.=.), gen(dilma21 dilma22)
recode disc_family_vote2 disc_friend_vote2 (2=1) (1=0) (3/14=0) (.=.), gen(aecio21 aecio22)
recode disc_family_vote2 disc_friend_vote2 (3=1) (1/2=0) (4/14=0) (.=.), gen(campos21 campos22)
recode disc_family_vote2 disc_friend_vote2 (4=1) (1/3=0) (5/14=0) (.=.), gen(marina21 marina22)
recode disc_family_vote2 disc_friend_vote2 (5=1) (1/4=0) (6/14=0) (.=.), gen(pastor21 pastor22)
recode disc_family_vote2 disc_friend_vote2 (6=1) (1/5=0) (7/14=0) (.=.), gen(jorge21 jorge22)
recode disc_family_vote2 disc_friend_vote2 (7=1) (1/6=0) (8/14=0) (.=.), gen(luciana21 luciana22)
recode disc_family_vote2 disc_friend_vote2 (8=1) (1/7=0) (9/14=0) (.=.), gen(other21 other22)
egen totopinionateddisc2=rsum(dilma21 dilma22 aecio21 aecio22 campos21 campos22 marina21 marina22 pastor21 pastor22 jorge21 jorge22 luciana21 luciana22 other21 other22)
replace totopinionateddisc2=. if respond2~=1
recode totopinionateddisc2 0=1 .=. else=0, gen(zerodiscstrong2)

				*wave 3
recode disc_family_vote3 disc_friend_vote3 (1=1) (2/14=0) (.=.), gen(dilma31 dilma32)
recode disc_family_vote3 disc_friend_vote3 (2=1) (1=0) (3/14=0) (.=.), gen(aecio31 aecio32)
recode disc_family_vote3 disc_friend_vote3 (3=1) (1/2=0) (4/14=0) (.=.), gen(campos31 campos32)
recode disc_family_vote3 disc_friend_vote3 (4=1) (1/3=0) (5/14=0) (.=.), gen(marina31 marina32)
recode disc_family_vote3 disc_friend_vote3 (5=1) (1/4=0) (6/14=0) (.=.), gen(pastor31 pastor32)
recode disc_family_vote3 disc_friend_vote3 (6=1) (1/5=0) (7/14=0) (.=.), gen(jorge31 jorge32)
recode disc_family_vote3 disc_friend_vote3 (7=1) (1/6=0) (8/14=0) (.=.), gen(luciana31 luciana32)
recode disc_family_vote3 disc_friend_vote3 (8=1) (1/7=0) (9/14=0) (.=.), gen(other31 other32)
egen totopinionateddisc3=rsum(dilma31 dilma32 aecio31 aecio32 campos31 campos32 marina31 marina32 pastor31 pastor32 jorge31 jorge32 luciana31 luciana32 other31 other32)
replace totopinionateddisc3=. if respond3~=1
recode totopinionateddisc3 0=1 .=. else=0, gen(zerodiscstrong3)

				*wave 4
recode disc_family_vote4 disc_friend_vote4 (1=1) (2/14=0) (.=.), gen(dilma41 dilma42)
recode disc_family_vote4 disc_friend_vote4 (2=1) (1=0) (3/14=0) (.=.), gen(aecio41 aecio42)
recode disc_family_vote4 disc_friend_vote4 (3=1) (1/2=0) (4/14=0) (.=.), gen(campos41 campos42)
recode disc_family_vote4 disc_friend_vote4 (4=1) (1/3=0) (5/14=0) (.=.), gen(marina41 marina42)
recode disc_family_vote4 disc_friend_vote4 (5=1) (1/4=0) (6/14=0) (.=.), gen(pastor41 pastor42)
recode disc_family_vote4 disc_friend_vote4 (6=1) (1/5=0) (7/14=0) (.=.), gen(jorge41 jorge42)
recode disc_family_vote4 disc_friend_vote4 (7=1) (1/6=0) (8/14=0) (.=.), gen(luciana41 luciana42)
recode disc_family_vote4 disc_friend_vote4 (8=1) (1/7=0) (9/14=0) (.=.), gen(other41 other42)
egen totopinionateddisc4=rsum(dilma41 dilma42 aecio41 aecio42 campos41 campos42 marina41 marina42 pastor41 pastor42 jorge41 jorge42 luciana41 luciana42 other41 other42)
replace totopinionateddisc4=. if respond4~=1
recode totopinionateddisc4 0=1 .=. else=0, gen(zerodiscstrong4)

		*wave 5
recode disc_family_vote5 disc_friend_vote5 (1=1) (2/14=0) (.=.), gen(dilma51 dilma52)
recode disc_family_vote5 disc_friend_vote5 (2=1) (1=0) (3/14=0) (.=.), gen(aecio51 aecio52)
recode disc_family_vote5 disc_friend_vote5 (3=1) (1/2=0) (4/14=0) (.=.), gen(campos51 campos52)
recode disc_family_vote5 disc_friend_vote5 (4=1) (1/3=0) (5/14=0) (.=.), gen(marina51 marina52)
recode disc_family_vote5 disc_friend_vote5 (5=1) (1/4=0) (6/14=0) (.=.), gen(pastor51 pastor52)
recode disc_family_vote5 disc_friend_vote5 (6=1) (1/5=0) (7/14=0) (.=.), gen(jorge51 jorge52)
recode disc_family_vote5 disc_friend_vote5 (7=1) (1/6=0) (8/14=0) (.=.), gen(luciana51 luciana52)
recode disc_family_vote5 disc_friend_vote5 (8=1) (1/7=0) (9/14=0) (.=.), gen(other51 other52)
egen totopinionateddisc5=rsum(dilma51 dilma52 aecio51 aecio52 campos51 campos52 marina51 marina52 pastor51 pastor52 jorge51 jorge52 luciana51 luciana52 other51 other52)
replace totopinionateddisc5=. if respond5~=1
recode totopinionateddisc5 0=1 .=. else=0, gen(zerodiscstrong5)
		*wave 6
recode disc_family_vote6 disc_friend_vote6 (1=1) (2/14=0) (.=.), gen(dilma61 dilma62)
recode disc_family_vote6 disc_friend_vote6 (2=1) (1=0) (3/14=0) (.=.), gen(aecio61 aecio62)
recode disc_family_vote6 disc_friend_vote6 (3=1) (1/2=0) (4/14=0) (.=.), gen(campos61 campos62)
recode disc_family_vote6 disc_friend_vote6 (4=1) (1/3=0) (5/14=0) (.=.), gen(marina61 marina62)
recode disc_family_vote6 disc_friend_vote6 (5=1) (1/4=0) (6/14=0) (.=.), gen(pastor61 pastor62)
recode disc_family_vote6 disc_friend_vote6 (6=1) (1/5=0) (7/14=0) (.=.), gen(jorge61 jorge62)
recode disc_family_vote6 disc_friend_vote6 (7=1) (1/6=0) (8/14=0) (.=.), gen(luciana61 luciana62)
recode disc_family_vote6 disc_friend_vote6 (8=1) (1/7=0) (9/14=0) (.=.), gen(other61 other62)
egen totopinionateddisc6=rsum(dilma61 dilma62 aecio61 aecio62 campos61 campos62 marina61 marina62 pastor61 pastor62 jorge61 jorge62 luciana61 luciana62 other61 other62)
replace totopinionateddisc6=. if respond6~=1
recode totopinionateddisc6 0=1 .=. else=0, gen(zerodiscstrong6)

*paradox thing
svy: prop disc_family_vote2 if disc_family_vote2 <14
svy: prop disc_friend_vote2 if disc_friend_vote2 <14
svy: prop vote2 if vote2<16

svy: prop disc_family_vote3 if disc_family_vote3 <14
svy: prop disc_friend_vote3 if disc_friend_vote3 <14
svy: prop vote3 if vote3<16

svy: prop disc_family_vote4 if disc_family_vote4 <14
svy: prop disc_friend_vote4 if disc_friend_vote4 <14
svy: prop vote4 if vote4<16

svy: prop disc_family_vote5 if disc_family_vote5 <14
svy: prop disc_friend_vote5 if disc_friend_vote5 <14
svy: prop vote5 if vote5<16

svy: mean dilma31 aecio31 marina31 if totopinionateddisc3>0 & totopinionateddisc3<3 
svy: mean dilma32 aecio32 marina32 if totopinionateddisc3>0 & totopinionateddisc3<3 
svy: prop vote3 if vote3<16

svy: mean dilma41 aecio41 marina41 if totopinionateddisc4>0 & totopinionateddisc4<3 
svy: mean dilma42 aecio42 marina42 if totopinionateddisc4>0 & totopinionateddisc4<3 
svy: prop vote4 if vote4<16

svy: mean dilma51 aecio51 marina51 if totopinionateddisc5>0 & totopinionateddisc5<3 
svy: mean dilma52 aecio52 marina52 if totopinionateddisc5>0 & totopinionateddisc5<3 
svy: prop vote5 if vote5<16


svyset [pweight=weight_censo1], psu(uf1)

			*for Weak disagreement (all discussants, absence of agreement)
				*wave 2
recode disc_family_vote2 disc_friend_vote2 (13/14=1) (1/12=0) (.a=1) (.b=1) (.c=.) (.=.), gen(noop21 noop22)
recode disc_total2 0=1 .=. else=0, gen(zerodiscweak2)
replace zerodiscweak2=. if respond2==0

				*wave 3
recode disc_family_vote3 disc_friend_vote3 (13/14=1) (1/12=0) (.a=1) (.b=1) (.c=.) (.=.), gen(noop31 noop32)
recode disc_total3 0=1 .=. else=0, gen(zerodiscweak3)
replace zerodiscweak3=. if respond3==0

				*wave 4
recode disc_family_vote4 disc_friend_vote4 (13/14=1) (1/12=0) (.a=1) (.b=1) (.c=.) (.=.), gen(noop41 noop42)
recode disc_total4 0=1 .=. else=0, gen(zerodiscweak4)
replace zerodiscweak4=. if respond4==0

				*wave 5
recode disc_family_vote5 disc_friend_vote5 (13/14=1) (1/12=0) (.a=1) (.b=1) (.c=.) (.=.), gen(noop51 noop52)
recode disc_total5 0=1 .=. else=0, gen(zerodiscweak5)
replace zerodiscweak5=. if respond5==0

				*wave 6
recode disc_family_vote6 disc_friend_vote6 (13/14=1) (1/12=0) (.a=1) (.b=1) (.c=.) (.=.), gen(noop61 noop62)
egen disc_total6weak=rsum(dilma61 dilma62 aecio61 aecio62 campos61 campos62 marina61 marina62 pastor61 pastor62 jorge61 jorge62 luciana61 luciana62 other61 other62 noop61 noop62)
replace disc_total6weak=. if respond6==0 
drop disc_total6
rename disc_total6weak disc_total6
egen noopsum_numerator6=rsum(noop61 noop62)
recode disc_total6 0=1 .=. else=0, gen(zerodiscweak6)
replace zerodiscweak6=. if respond6==0

	*Agreement and disagreement
recode disc_family_vote* disc_friend_vote* (8=15)
gen agree21=0
replace agree21=1 if vote2==disc_family_vote2
replace agree21=. if vote2>15
replace agree21=. if disc_family_vote2>15
gen agree22=0
replace agree22=1 if vote2==disc_friend_vote2
replace agree22=. if vote2>15
replace agree22=. if disc_friend_vote2>15

replace agree21=. if respond2~=1
replace agree22=. if respond2~=1
egen numagree2=rsum(agree21-agree22)
replace numagree2=. if respond2==0
replace numagree2=. if vote2>15
			
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong2=1-numagree2/totopinionateddisc2
replace disagreementstrong2=0 if totopinionateddisc2==0
replace disagreementstrong2=. if vote2>15
gen heterogeneitystrong2=0 if disagreementstrong2~=.
replace heterogeneitystrong2=.5 if disc_family_vote2~=disc_friend_vote2 & disagreementstrong2==1 & totopinionateddisc2==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak2=1-numagree2/disc_total2
replace disagreementweak2=0 if disc_total2==0
replace disagreementweak2=. if vote2>15
gen heterogeneityweak2=0 if disagreementweak2~=.
replace heterogeneityweak2=.5 if disc_family_vote2~=disc_friend_vote2 & disagreementweak2==1 & disc_total2==2  
replace heterogeneityweak2=0 if (heterogeneityweak2==.5 & disc_family_vote2==14 & disc_friend_vote2==.a) | (heterogeneityweak2==.5 & disc_friend_vote2==14  & disc_family_vote2==.a) 

gen agree31=0
replace agree31=1 if vote3==disc_family_vote3
replace agree31=. if vote3>15
replace agree31=. if disc_family_vote3>15
gen agree32=0
replace agree32=1 if vote3==disc_friend_vote3
replace agree32=. if vote3>15
replace agree32=. if disc_friend_vote3>15

replace agree31=. if respond3~=1
replace agree32=. if respond3~=1
egen numagree3=rsum(agree31-agree32)
replace numagree3=. if respond3==0
replace numagree3=. if vote3>15
	
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong3=1-numagree3/totopinionateddisc3
replace disagreementstrong3=0 if totopinionateddisc3==0
replace disagreementstrong3=. if vote3>15
gen heterogeneitystrong3=0 if disagreementstrong3~=.
replace heterogeneitystrong3=.5 if disc_family_vote3~=disc_friend_vote3 & disagreementstrong3==1 & totopinionateddisc3==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak3=1-numagree3/disc_total3
replace disagreementweak3=0 if disc_total3==0
replace disagreementweak3=. if vote3>15
gen heterogeneityweak3=0 if disagreementweak3~=.
replace heterogeneityweak3=.5 if disc_family_vote3~=disc_friend_vote3 & disagreementweak3==1 & disc_total3==2  
replace heterogeneityweak3=0 if (heterogeneityweak3==.5 & disc_family_vote3==14 & disc_friend_vote3==.a) | (heterogeneityweak3==.5 & disc_friend_vote3==14  & disc_family_vote3==.a) 

gen agree41=0
replace agree41=1 if vote4==disc_family_vote4
replace agree41=. if vote4>15
replace agree41=. if disc_family_vote4>15
gen agree42=0
replace agree42=1 if vote4==disc_friend_vote4
replace agree42=. if vote4>15
replace agree42=. if disc_friend_vote4>15

replace agree41=. if respond4~=1
replace agree42=. if respond4~=1
egen numagree4=rsum(agree41-agree42)
replace numagree4=. if respond4==0
replace numagree4=. if vote4>15
			
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong4=1-numagree4/totopinionateddisc4
replace disagreementstrong4=0 if totopinionateddisc4==0
replace disagreementstrong4=. if vote4>15
gen heterogeneitystrong4=0 if disagreementstrong4~=.
replace heterogeneitystrong4=.5 if disc_family_vote4~=disc_friend_vote4 & disagreementstrong4==1 & totopinionateddisc4==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak4=1-numagree4/disc_total4
replace disagreementweak4=0 if disc_total4==0
replace disagreementweak4=. if vote4>15
gen heterogeneityweak4=0 if disagreementweak4~=.
replace heterogeneityweak4=.5 if disc_family_vote4~=disc_friend_vote4 & disagreementweak4==1 & disc_total4==2  
replace heterogeneityweak4=0 if (heterogeneityweak4==.5 & disc_family_vote4==14 & disc_friend_vote4==.a) | (heterogeneityweak4==.5 & disc_friend_vote4==14  & disc_family_vote4==.a) 

gen agree51=0
replace agree51=1 if vote5==disc_family_vote5
replace agree51=. if vote5>15
replace agree51=. if disc_family_vote5>15
gen agree52=0
replace agree52=1 if vote5==disc_friend_vote5
replace agree52=. if vote5>15
replace agree52=. if disc_friend_vote5>15

replace agree51=. if respond5~=1
replace agree52=. if respond5~=1
egen numagree5=rsum(agree51-agree52)
replace numagree5=. if respond5==0
replace numagree5=. if vote5>15
			
tab agree51 if disc5a5<3
tab agree41 if disc5a4<3

			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong5=1-numagree5/totopinionateddisc5
replace disagreementstrong5=0 if totopinionateddisc5==0
replace disagreementstrong5=. if vote5>15
gen heterogeneitystrong5=0 if disagreementstrong5~=.
replace heterogeneitystrong5=.5 if disc_family_vote5~=disc_friend_vote5 & disagreementstrong5==1 & totopinionateddisc5==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak5=1-numagree5/disc_total5
replace disagreementweak5=0 if disc_total5==0
replace disagreementweak5=. if vote5>15
gen heterogeneityweak5=0 if disagreementweak5~=.
replace heterogeneityweak5=.5 if disc_family_vote5~=disc_friend_vote5 & disagreementweak5==1 & disc_total5==2  
replace heterogeneityweak5=0 if (heterogeneityweak5==.5 & disc_family_vote5==14 & disc_friend_vote5==.a) | (heterogeneityweak5==.5 & disc_friend_vote5==14  & disc_family_vote5==.a) 

gen agree612=0
replace agree612=1 if vote2==disc_family_vote6
replace agree612=. if vote2>15
replace agree612=. if disc_family_vote6>15
gen agree622=0
replace agree622=1 if vote2==disc_friend_vote6
replace agree622=. if vote2>15
replace agree622=. if disc_friend_vote6>15

replace agree612=. if respond6~=1
replace agree622=. if respond6~=1
egen numagree6_2=rsum(agree612-agree622)
replace numagree6_2=. if respond6==0
replace numagree6_2=. if vote2>15
replace numagree6_2=. if respond2==0
			
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong6_2=1-numagree6_2/totopinionateddisc6
replace disagreementstrong6_2=0 if totopinionateddisc6==0
replace disagreementstrong6_2=. if vote2>15
gen heterogeneitystrong6_2=0 if disagreementstrong6_2~=.
replace heterogeneitystrong6_2=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementstrong6_2==1 & totopinionateddisc6==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak6_2=1-numagree6_2/disc_total6
replace disagreementweak6_2=0 if disc_total6==0
replace disagreementweak6_2=. if vote2>15
gen heterogeneityweak6_2=0 if disagreementweak6_2~=.
replace heterogeneityweak6_2=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementweak6_2==1 & disc_total6==2  
replace heterogeneityweak6_2=0 if (heterogeneityweak6_2==.5 & disc_family_vote6==14 & disc_friend_vote6==.a) | (heterogeneityweak6_2==.5 & disc_friend_vote6==14  & disc_family_vote6==.a) 

gen agree613=0
replace agree613=1 if vote3==disc_family_vote6
replace agree613=. if vote3>15
replace agree613=. if disc_family_vote6>15
gen agree623=0
replace agree623=1 if vote3==disc_friend_vote6
replace agree623=. if vote3>15
replace agree623=. if disc_friend_vote6>15

replace agree613=. if respond6~=1
replace agree623=. if respond6~=1
egen numagree6_3=rsum(agree613-agree623)
replace numagree6_3=. if respond6==0
replace numagree6_3=. if vote3>15
replace numagree6_3=. if respond3==0
			
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong6_3=1-numagree6_3/totopinionateddisc6
replace disagreementstrong6_3=0 if totopinionateddisc6==0
replace disagreementstrong6_3=. if vote3>15
gen heterogeneitystrong6_3=0 if disagreementstrong6_3~=.
replace heterogeneitystrong6_3=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementstrong6_3==1 & totopinionateddisc6==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak6_3=1-numagree6_3/disc_total6
replace disagreementweak6_3=0 if disc_total6==0
replace disagreementweak6_3=. if vote3>15
gen heterogeneityweak6_3=0 if disagreementweak6_3~=.
replace heterogeneityweak6_3=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementweak6_3==1 & disc_total6==2  
replace heterogeneityweak6_3=0 if (heterogeneityweak6_3==.5 & disc_family_vote6==14 & disc_friend_vote6==.a) | (heterogeneityweak6_3==.5 & disc_friend_vote6==14  & disc_family_vote6==.a) 

gen agree614=0
replace agree614=1 if vote4==disc_family_vote6
replace agree614=. if vote4>15
replace agree614=. if disc_family_vote6>15
gen agree624=0
replace agree624=1 if vote4==disc_friend_vote6
replace agree624=. if vote4>15
replace agree624=. if disc_friend_vote6>15

replace agree614=. if respond6~=1
replace agree624=. if respond6~=1
egen numagree6_4=rsum(agree614-agree624)
replace numagree6_4=. if respond6==0
replace numagree6_4=. if vote4>15
replace numagree6_4=. if respond4==0			
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong6_4=1-numagree6_4/totopinionateddisc6
replace disagreementstrong6_4=0 if totopinionateddisc6==0
replace disagreementstrong6_4=. if vote4>15
gen heterogeneitystrong6_4=0 if disagreementstrong6_4~=.
replace heterogeneitystrong6_4=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementstrong6_4==1 & totopinionateddisc6==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak6_4=1-numagree6_4/disc_total6
replace disagreementweak6_4=0 if disc_total6==0
replace disagreementweak6_4=. if vote4>15
gen heterogeneityweak6_4=0 if disagreementweak6_4~=.
replace heterogeneityweak6_4=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementweak6_4==1 & disc_total6==2  
replace heterogeneityweak6_4=0 if (heterogeneityweak6_4==.5 & disc_family_vote6==14 & disc_friend_vote6==.a) | (heterogeneityweak6_4==.5 & disc_friend_vote6==14  & disc_family_vote6==.a) 

gen agree615=0
replace agree615=1 if vote5==disc_family_vote6
replace agree615=. if vote5>15
replace agree615=. if disc_family_vote6>15
gen agree625=0
replace agree625=1 if vote5==disc_friend_vote6
replace agree625=. if vote5>15
replace agree625=. if disc_friend_vote6>15

replace agree615=. if respond6~=1
replace agree625=. if respond6~=1
egen numagree6_5=rsum(agree615-agree625)
replace numagree6_5=. if respond6==0
replace numagree6_5=. if vote5>15
			
			*Strong disagreement is absence of agreement over candidates (Sokhey et al AJPS)
gen disagreementstrong6_5=1-numagree6_5/totopinionateddisc6
replace disagreementstrong6_5=0 if totopinionateddisc6==0
replace disagreementstrong6_5=. if vote5>15
gen heterogeneitystrong6_5=0 if disagreementstrong6_5~=.
replace heterogeneitystrong6_5=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementstrong6_5==1 & totopinionateddisc6==2

			*Weak disagreement is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak6_5=1-numagree6_5/disc_total6
replace disagreementweak6_5=0 if disc_total6==0
replace disagreementweak6_5=. if vote5>15
gen heterogeneityweak6_5=0 if disagreementweak6_5~=.
replace heterogeneityweak6_5=.5 if disc_family_vote6~=disc_friend_vote6 & disagreementweak6_5==1 & disc_total6==2  
replace heterogeneityweak6_5=0 if (heterogeneityweak6_5==.5 & disc_family_vote6==14 & disc_friend_vote6==.a) | (heterogeneityweak6_5==.5 & disc_friend_vote6==14  & disc_family_vote6==.a) 


*-----------------------------------------------------------------------------------------------------------------------------------------
*STATS on changing in unison
recode disc_family_vote4 disc_friend_vote4 (4=1) (1/3=0) (5/14=0) (else=.), gen(marina41temp marina42temp)
recode disc_family_vote5 disc_friend_vote5 (4=1) (1/3=0) (5/14=0) (else=.), gen(marina51temp marina52temp)

egen marina_temp45=rowtotal(marina41temp marina42temp marina51temp marina52temp)
egen denom45=rowtotal(totopinionateddisc4 totopinionateddisc5)
gen marina_unam45=marina_temp45/denom45

replace marina_unam45=0 if marina_unam45<1

recode disc_family_vote6 disc_friend_vote6 (1=1) (2/14=0) (else=.), gen(dilma61temp dilma62temp)
recode disc_family_vote6 disc_friend_vote6 (2=1) (1=0) (3/14=0) (else=.), gen(aecio61temp aecio62temp)

egen dilma_temp6=rowtotal(dilma61temp dilma62temp )
egen aecio_temp6=rowtotal(aecio61temp aecio62temp )

gen dilma_unam6=dilma_temp6/totopinionateddisc6
gen aecio_unam6=aecio_temp6/totopinionateddisc6
	
egen unam6=rowmax(dilma_unam6 aecio_unam6)
replace unam6=0 if unam6<1

tab marina_unam45 unam6 if vote4==4 & change456==1,m
tab marina_unam45 unam6 if vote5==4 & change456==1,m

bysort change456: tab marina_unam45 unam6 if vote4==4 & change456~=.,m
bysort change456: tab marina_unam45 unam6 if vote5==4 & change456~=.,m

*OTHER IVs

*egen awarez=std(pkw11)
	*I added in ideo and ft because the set of pk items is severely truncated at the bottom. 43% didn't know any.
recode pk11 pk21 pk31 pk41 pk51 (.a=0) (.b=0) (2=0)
recode ideo1 (0/10=1) (.a=0) (.b=0) (.=.), gen(ideo_respond1) 
recode ft11-ft71 (0/10=1) (.a=0) (.b=0) (.=.)
egen ft_know=anycount(ft11 ft21 ft31 ft41 ft51 ft61 ft71), values(1)
svy: irt grm pk11 pk21 pk31 pk41 pk51 ideo_respond1 ft_know
predict aware_irt, latent ebmeans
egen awarez=std(aware_irt)

preserve
keep idtelefone awarez
sort idtelefone
save "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\awarez 2014.dta", replace
restore

recode med12 med13 med14 med15 med16 med17 (5=1) (4=2) (3=3) (2=4) (1=5), gen(media2 media3 media4 media5 media6 media7) 
egen mediaattention=rmean(med12 med13 med14 med15 med16 med17)

egen hgpe=rsum(hpge1_14 hpge1_24 hpge1_15 hpge1_25)
replace hgpe=. if hpge1_14==. & hpge1_24==. & hpge1_15==. & hpge1_25==.

*opening anecdote
svy: tab hgpe vote6 if vote3==4, row
svy: tab hgpe vote6 if vote4==4, row
svy: tab hgpe vote6 if vote5==4, row


*Smush together wave 2 and 3 answers
egen change2or36=rmax(change26 change36)
egen disagreementstrong2or3=rmax(disagreementstrong2 disagreementstrong3)
egen disagreementstrong6_2or3=rmax(disagreementstrong6_2 disagreementstrong6_3)
egen zerodiscstrong2or3=rmax(zerodiscstrong2 zerodiscstrong3)
egen heterogeneitystrong2or3=rmax(heterogeneitystrong2 heterogeneitystrong3)
egen heterogeneitystrong6_2or3=rmax(heterogeneitystrong6_2 heterogeneitystrong6_3)

egen disagreementweak2or3=rmax(disagreementweak2 disagreementweak3)
egen disagreementweak6_2or3=rmax(disagreementweak6_2 disagreementweak6_3)
egen zerodiscweak2or3=rmax(zerodiscweak2 zerodiscweak3)
egen heterogeneityweak2or3=rmax(heterogeneityweak2 heterogeneityweak3)
egen heterogeneityweak6_2or3=rmax(heterogeneityweak6_2 heterogeneityweak6_3)
egen threshold2or3=rowmean(threshold2 threshold3 thresholdft2 thresholdft3)

*Smush together wave 4 and 5 answers
egen change4or56=rmax(change46 change56)
egen disagreementstrong4or5=rmax(disagreementstrong4 disagreementstrong5)
egen disagreementstrong6_4or5=rmax(disagreementstrong6_4 disagreementstrong6_5)
egen zerodiscstrong4or5=rmax(zerodiscstrong4 zerodiscstrong5)
egen heterogeneitystrong4or5=rmax(heterogeneitystrong4 heterogeneitystrong5)
egen heterogeneitystrong6_4or5=rmax(heterogeneitystrong6_4 heterogeneitystrong6_5)

egen disagreementweak4or5=rmax(disagreementweak4 disagreementweak5)
egen disagreementweak6_4or5=rmax(disagreementweak6_4 disagreementweak6_5)
egen zerodiscweak4or5=rmax(zerodiscweak4 zerodiscweak5)
egen heterogeneityweak4or5=rmax(heterogeneityweak4 heterogeneityweak5)
egen heterogeneityweak6_4or5=rmax(heterogeneityweak6_4 heterogeneityweak6_5)
egen threshold4or5=rowmean(threshold4 threshold5 thresholdft4 thresholdft5)
 
gen vote45=vote4
replace vote45=vote5 if vote4==. & respond4==0 & respond5==1

 	*GET QUICK CROSSTABS
preserve
replace disagreementstrong4or5=-1 if zerodiscstrong4or5==1
svy: tab disc_total45 if (respond4==1 & vote45<16) | (respond5==1 & vote45<16) 
svy: tab disagreementstrong4or5 if (respond4==1 & vote45<16) | (respond5==1 & vote45<16) 
svy: tab disagreementstrong4or5 heterogeneitystrong4or5 if (respond4==1 & vote45<16) | (respond5==1 & vote45<16) , cel
restore

label variable disagreementstrong6_2or3 "Network disagreement (strong)e wave"
label variable disagreementstrong6_4or5 "Network disagreement (strong)e wave"
label variable disagreementstrong4or5 "Network disagreement (strong)c wave2"
label variable disagreementstrong2or3 "Network disagreement (strong)c wave1"

label variable zerodiscstrong6 "Isolates (strong)e wave"
label variable zerodiscstrong4or5 "Isolates (strong)c wave2"
label variable zerodiscstrong2or3 "Isolates (strong)c wave1"

label variable disagreementweak6_2or3 "Network disagreement (weak)e wave"
label variable disagreementweak6_4or5 "Network disagreement (weak)e wave"
label variable disagreementweak4or5 "Network disagreement (weak)c wave2"
label variable disagreementweak2or3 "Network disagreement (weak)c wave1"

label variable zerodiscweak4or5 "Isolates (weak)c wave2"
label variable zerodiscweak2or3 "Isolates (weak)c wave1"

label variable awarez "Political awareness"
label variable partyintense1 "Threshold: strength of partisanship"
label variable threshold4or5 "Threshold: candidate evaluations4or5"
label variable threshold2or3 "Threshold: candidate evaluations2or3"

label variable change4or56 "DV: Changed vote intention (between w4/w5 and w6)"
label variable change2or36 "DV: Changed vote intention (between w2/w3 and w6)"

*Aggregate
/*
gen vote45=vote4
replace vote45=vote5 if vote4==. & respond4==0 & respond5==1
recode vote45 vote6 (16/17=.), gen(vote45ag vote6ag) 
label values vote45ag vote6ag vote
svy: tab vote45ag vote6ag, row
mean disagreementstrong4or5 [pweight=weight_censo1] , over(vote45ag)

svy: logit change4or56 awarez 
*margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

svy: reg change4or56 awarez i.vote45 
*margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
*/

*eststo clear

svy: logit changedatall awarez i.vote1 i.vote45 
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

recode vote1 1=1 .=. else=0, gen(dilma)
	
svy: logit switchedatall awarez dilma
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

svy: mean awarez if vote6==1 & switchedatall==0 
svy: mean awarez if vote6==1 & switchedatall==1

svy: mean awarez if vote6==2 & switchedatall==0
svy: mean awarez if vote6==2 & switchedatall==1

svy: mean awarez if vote6==4 & switchedatall==0
svy: mean awarez if vote6==4 & switchedatall==1

	*timing
		*1 to 4/5 to 6
recode vote1 3=4 .=16, gen(vote1temp)
gen timing=1 if vote1temp==vote45 & vote1temp==vote6 & vote45==vote6 & vote1temp~=. & vote45~=. & vote6~=.
replace timing=2 if vote1temp~=vote45 & vote45==vote6 & vote1temp~=. & vote45~=. & vote6~=.
replace timing=3 if vote45~=vote6 & vote1temp~=. & vote45~=. & vote6~=. 
replace timing=. if vote6>7

svy: ologit timing awarez respond4
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
margin, at((min) awarez) atmeans
margin, at((max) awarez) atmeans

margin, at((max) awarez) at((min) awarez) atmeans
marginsplot, xdimension(_outcome) yscale(range(0 .6)) ylab(0 "0%" .1 "10%" .2 "20%" .3 "30%" .4 "40%" .5 "50%" .6 "60%") xscale(range(.8 3.2)) ///
ytitle("{bf:Model-Predicted}" "{bf:Percentage of Voters}") legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xlab(1 "Before campaign" 2 "Before late campaign" 3 "Before election day") xtitle("{bf:When Decided?}") ///
title("") plot1opts(msymbol(0) lcolor(black) mcolor(black) ) plot2opts(msymbol(S) lcolor(gs7) mcolor(gs7) ) ci1opts(color(black)) ci2opts(color(gs7)) ///
text(.465 1.39 "Max knowledge") text(.39 2.63 "Min knowledge", color(gs7))
graph save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014timing.gph", replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014timing.tif", as(tif) replace
*/

		*1 to 2/3 to 6
recode vote2 3=4 .=16, gen(vote2temp)
gen vote23=vote2temp
replace vote23=vote3 if respond2==0 & respond3==1
replace vote23=. if respond2==0 & respond3==0

gen timing2=1 if vote1temp==vote23 & vote1temp==vote6 & vote23==vote6 & vote1temp~=. & vote23~=. & vote6~=.
replace timing2=2 if vote1temp~=vote23 & vote23==vote6 & vote1temp~=. & vote23~=. & vote6~=.
replace timing2=3 if vote23~=vote6 & vote1temp~=. & vote23~=. & vote6~=. 
replace timing2=. if vote6>7
*Table A.11
svy: ologit timing2 awarez respond2
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
margin, at((min) awarez) atmeans
margin, at((max) awarez) atmeans

*Figure 4.9A
margin, at((max) awarez) at((min) awarez) atmeans
marginsplot, xdimension(_outcome) yscale(range(0 .6)) ylab(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%") xscale(range(.8 3.2)) ///
ytitle("{bf:Percentage of Voters}") legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xlab(1 "Bef. campaign" 2 "Bef. mid-campaign" 3 "Bef. election day", labsize(small)) xtitle("{bf:Timing of Vote Decision}") ///
title("{bf:          Brazil 2014}", color(black) size(medsmall)) plot1opts(msymbol(0) lcolor(black) mcolor(black) ) plot2opts(msymbol(S) lcolor(gs7) mcolor(gs7) ) ci1opts(color(black)) ci2opts(color(gs7)) ///
text(.425 1.39 "Max knowledge", size(small)) text(.605 2.62 "Min knowledge", color(gs7) size(small)) text(0 1 "{it:N}=697", size(small)) 
graph save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014timing2.gph", replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014timing2.tif", as(tif) replace

recode timing2 1=0 2/3=1, gen(switchedatall23)
*Figure 4.9B, Table A.11, 
svy: logit switchedatall23 awarez respond2
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

recode timing2 1=0 2/3=1, gen(changedatall23)
replace changedatall23=. if vote1>11
replace changedatall23=. if vote2==16 | vote2==17
replace changedatall23=. if vote3==16 | vote3==17

svy: logit changedatall23 awarez respond2 i.vote1 i.vote23
*margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
xyz


cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
*esttab est1 est2 using figure49c.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Vote Switching by Political Knowledge)

*eststo clear
label variable disagreementstrong4or5 "Network disagreement"
label variable zerodiscstrong4or5 "Singleton"

*eststo: logit change4or56 disagreementstrong4or5 zerodiscstrong4or5 [pweight=weight_censo1], cluster(uf1)
*Figure 4.3, Table A.3
logit change4or56 disagreementstrong4or5 zerodiscstrong4or5 [pweight=weight_censo1], cluster(uf1)
margin, at(disagreementstrong4or5=1 zerodiscstrong4or5==0 )
margin, at(disagreementstrong4or5=.5 zerodiscstrong4or5==0 )
margin, at(disagreementstrong4or5=0 zerodiscstrong4or5==1 )
margin, at(disagreementstrong4or5=0 zerodiscstrong4or5==0 )

*esttab est1 using figure43c.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Changed Vote Preference between Campaign and Election Waves by Campaign-Wave Network Traits: Results from Four Binary Logit Models )

/*
margin, at(disagreementstrong4or5=1 heterogeneitystrong4or5=0 zerodiscstrong4or5==0 )
margin, at(disagreementstrong4or5=1 heterogeneitystrong4or5=.5 zerodiscstrong4or5==0 )
margin, at(disagreementstrong4or5=.5 heterogeneitystrong4or5=0 zerodiscstrong4or5==0 )
margin, at(disagreementstrong4or5=0 heterogeneitystrong4or5=0 zerodiscstrong4or5==1 )
margin, at(disagreementstrong4or5=0 heterogeneitystrong4or5=0 zerodiscstrong4or5==0 )
*/
*/

*AGGREGATE 
preserve
	*Partisan intensity
recode vb111 (1501=1) (else=0), gen(partyintensePT1)
replace partyintensePT1=.5 if vb131==1501
svy: mean partyintensePT1
mat bpt=e(b)

recode vb111 (1503=1) (else=0), gen(partyintensePSDB1)
replace partyintensePSDB1=.5 if vb131==1503
svy: mean partyintensePSDB1
mat bpsdb=e(b)

recode vb111 (1504=1) (else=0), gen(partyintensePSB1)
replace partyintensePSB1=.5 if vb131==1504
svy: mean partyintensePSB1
mat bpsb=e(b)

svmat bpt, names(ptintensetemp)
svmat bpsdb, names(psdbintensetemp)
svmat bpsb, names(psbintensetemp)
egen partyintensePT=mean(ptintensetemp)
egen partyintensePSDB=mean(psdbintensetemp)
egen partyintensePSB=mean(psbintensetemp)

	*Stability
gen presvote45t=vote5
*replace presvote45t=vote5 if vote4==. & respond4==0 & respond5==1
recode presvote45t vote6 (16/17=.), gen(presvote45 presvote6) 
svy: tab presvote45 presvote6, row
mean disagreementstrong4or5 [pweight=weight_censo1] , over(presvote45)

gen dilmastability=0 if presvote45==1 & presvote6~=.
replace dilmastability=1 if presvote45==1 & presvote6==1

gen aeciostability=0 if presvote45==2 & presvote6~=.
replace aeciostability=1 if presvote45==2 & presvote6==2

gen marinastability=0 if presvote45==4 & presvote6~=.
replace marinastability=1 if presvote45==4 & presvote6==4

*to get complement of their vote total in campaign wave
recode presvote45 1=0 .=. else=1, gen(dilmavotetemp1)
svy: mean dilmavotetemp1
mat bpt2=e(b)
recode presvote45 2=0 .=. else=1, gen(aeciovotetemp1)
svy: mean aeciovotetemp1
mat bpsdb2=e(b)
recode presvote45 4=0 .=. else=1, gen(marinavotetemp1)
svy: mean marinavotetemp1
mat bpsb2=e(b)
svmat bpt2, names(dilmavotetemp1)
svmat bpsdb2, names(aeciovotetemp1)
svmat bpsb2, names(marinavotetemp1)
egen dilmavote=mean(dilmavotetemp1)
egen aeciovote=mean(aeciovotetemp1)
egen marinavote=mean(marinavotetemp1)

replace dilma_therm4=. if presvote45~=1
replace aecio_therm4=. if presvote45~=2
replace marina_therm4=. if presvote45~=4
replace dilmavote=. if presvote45~=1
replace aeciovote=. if presvote45~=2
replace marinavote=. if presvote45~=4
replace partyintensePT=. if presvote45~=1
replace partyintensePSDB=. if presvote45~=2
replace partyintensePSB=. if presvote45~=4

collapse disagreementstrong4or5 dilma_therm4 aecio_therm4 marina_therm4 dilmavote aeciovote marinavote dilmastability aeciostability marinastability partyintensePT partyintensePSDB partyintensePSB [pweight=weight_censo1], by(presvote45) 
keep if presvote45<5
egen threshold=rowmean(dilma_therm4 aecio_therm4 marina_therm4 )
egen votecomplement=rowmean(dilmavote aeciovote marinavote )
egen stability=rowmean(dilmastability aeciostability marinastability )
egen partyintense=rowmean(partyintensePT partyintensePSDB partyintensePSB )
gen year=2014
rename presvote45 presvote
rename disagreementstrong4or5 disagreementstrong
recode presvote 1=201 2=202 4=204
drop dilma_therm4 - partyintensePSB 
save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg14.dta", replace
restore

gen presvote23t=vote2
replace presvote23t=vote3 if vote2==. & respond2==0 & respond3==1
recode presvote23t (5/15=5) (16/17=.), gen(presvote23) 
gen presvote45t=vote4
replace presvote45t=vote5 if vote4==. & respond4==0 & respond5==1
recode presvote45t vote6 (5/15=5) (16/17=.), gen(presvote45 presvote6) 

*REGRESSIONS
	*Listwise deletion
eststo clear
constraint 1 heterogeneitystrong6_4or5==0
constraint 2 heterogeneitystrong4or5 ==0
eststo: logit change4or56 disagreementstrong6_4or5 disagreementstrong4or5 heterogeneitystrong6_4or5 heterogeneitystrong4or5 zerodiscstrong6 zerodiscstrong4or5 i.presvote45##c.mediaattention [pweight=weight_censo1], constraint(1,2) cluster(uf1)
margins, at(disagreementstrong6_4or5==0 disagreementstrong4or5==0 zerodiscstrong6==0 zerodiscstrong4or5=0) atmeans
margins , at(disagreementstrong6_4or5==1 disagreementstrong4or5==0 zerodiscstrong6==0 zerodiscstrong4or5=0) atmeans
margins , at(disagreementstrong6_4or5==0 disagreementstrong4or5==0 zerodiscstrong6==1 zerodiscstrong4or5=0) atmeans

*Figure 4.4C, Table A.4
coefplot (est1 , mcolor(black) ciopts(lcolor(black) recast(rcap))), ///
drop(_cons 2.presvote45 4.presvote45 5.presvote45) xline(0, lcolor(black)) ///
coeflabels(disagreementstrong6_4or5="Network.disagreement{sub:{it:e}}" disagreementstrong4or5="Network.disagreement{sub:{it:c}}"  ///
heterogeneitystrong6_4or5="Disagreeing.alters'.diversity{sub:{it:e}}"  heterogeneitystrong4or5="Disagreeing.alters'.diversity{sub:{it:c}}"  zerodiscstrong6="Singleton{sub:{it:e}}" zerodiscstrong4or5 ="Singleton{sub:{it:c}}" ///
mediaattention="Media exposure" 2.presvote45#c.mediaattention="Aécio intention×Media exposure"  ///
4.presvote45#c.mediaattention="Marina intention×Media exposure" 5.presvote45#c.mediaattention="Other intention×Media exposure", labsize(medsmall)) ///
legend(off) graphregion(color(white)) order() plotregion(lstyle(yxline) lcolor(black)) ///
msize(medsmall) groups(disagreementstrong6_4or5 disagreementstrong4or5 zerodiscstrong6 zerodiscstrong4or5 mediaattention 2.presvote45#c.mediaattention ///
4.presvote45#c.mediaattention 5.presvote45#c.mediaattention ="{bf:Independent Variables: Political Intermediaries}", labsize(medium)) ///
headings(disagreementstrong6_4or5= "{bf:Horizontal}" mediaattention = "{bf:Vertical}") ///
xscale(range(-3.5 3.5)) xlab(-4 -3 -2 -1 0 1 2 3 4) ///
xtitle("{bf:Logit Coefficient}", size(medium)) yscale(titlegap(52))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2014_4or5IndyCoefPlot.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4C.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4C.pdf", as(pdf) replace 

*Figure 4.4C, Table A.5
eststo: logit change4or56 disagreementweak6_4or5 disagreementweak4or5 heterogeneityweak6_4or5 heterogeneityweak4or5 zerodiscweak6 zerodiscweak4or5 i.presvote45##c.mediaattention [pweight=weight_censo1]
margins, at(disagreementweak6_4or5==0 disagreementweak4or5==0 heterogeneityweak6_4or5==0 heterogeneityweak4or5==0 zerodiscweak6==0 zerodiscweak4or5=0) atmeans
margins , at(disagreementweak6_4or5==1 disagreementweak4or5==0 heterogeneityweak6_4or5==0 heterogeneityweak4or5==0 zerodiscweak6==0 zerodiscweak4or5=0) atmeans
xyz
eststo: logit change2or36 disagreementstrong6_2or3 disagreementstrong2or3 zerodiscstrong6 zerodiscstrong2or3 i.presvote23##c.mediaattention  [pweight=weight_censo1]
margins, at(disagreementstrong6_2or3==0 disagreementstrong2or3==0 zerodiscstrong6==0 zerodiscstrong2or3=0) atmeans
margins , at(disagreementstrong6_2or3==1 disagreementstrong2or3==0 zerodiscstrong6==0 zerodiscstrong2or3=0) atmeans

*not going to do coefplot to reduce clutter. these are entered into table, however

eststo: logit change2or36 disagreementweak6_2or3 disagreementweak2or3 heterogeneityweak6_2or3 heterogeneityweak2or3 zerodiscweak6 zerodiscweak2or3 i.presvote23##c.mediaattention  [pweight=weight_censo1], constraint(3,4)
margins, at(disagreementweak6_2or3==0 disagreementweak2or3==0 heterogeneityweak6_2or3==0 heterogeneityweak2or3==0 zerodiscweak6==0 zerodiscweak2or3=0) atmeans
margins , at(disagreementweak6_2or3==1 disagreementweak2or3==0 heterogeneityweak6_2or3==0 heterogeneityweak2or3==0 zerodiscweak6==0 zerodiscweak2or3=0) atmeans

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est3 est4 est1 est2  using Brazil2014indy.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Switching in Brazil 2014) ///
modelwidth(6) nonumbers mtitles("wave2/3 to 6" "wave2/3 to 6" "wave4/5 to 6" "wave4/5 to 6") compress
